<?php
/*由脚本生成 powerBy--Reer*/

namespace app\common\manage;

use app\common\bean\BaseBean;
use app\common\bean\GlobalBean;
use app\common\bean\ListMap;
use app\common\bean\StoreRoleBean;
use app\common\bean\StoreUserBean;
use app\common\exception\MyException;
use app\common\model\StoreRoleModel;
use app\common\model\StoreUserModel;
use app\common\utils\TimeUtil;
use app\common\validate\StoreRoleValidate;

class StoreRoleManage extends BaseManage
{
    /**
     * @return StoreRoleManage
     */
    static function getInstance(){
        return parent::getInstance();
    }

    /** 获取角色选择select
     * @return array
     */
    public function getStoreRoleSelect($storeId){
        $allAdminRole = StoreRoleModel::getInstance()->selectData(array(StoreRoleBean::$storeId => $storeId),true);
        $adminRoleSelect = $this->modelToInputSelect($allAdminRole,StoreRoleBean::$id,StoreRoleBean::$name);
        return $adminRoleSelect;
    }

    /**
     * @desc 获取分页数据
     * @param ListMap $listMap
     * @return ListMap $listMap
     * @throws \Exception
     */
    public function getPaginatorList(ListMap $listMap)
    {
        // TODO: Implement getPaginatorList() method.
        $conditionMap = new ListMap();
        if($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap)){
            $conditionMap->setParameter(StoreRoleBean::$alias.POINST.StoreRoleBean::$createdAt,$timeArray);
        }
        $conditionMap->setParameter(StoreRoleBean::$alias.POINST.StoreRoleBean::$name,array("like" ,'%'.$listMap->getParameter(StoreRoleBean::$name).'%'));
        !$this->getParamBool($listMap,"orderFieldName") ?: $conditionMap->setParameter('order',StoreRoleBean::$alias.POINST.$listMap->getParameter('orderFieldName')." ".$listMap->getParameter('orderType'));
        $conditionMap->setAlias(StoreRoleBean::$alias);
        $conditionMap->setField(StoreRoleBean::$alias.POINST."*,count(".StoreUserBean::$alias.POINST.StoreUserBean::$id.") as user_count");
        $conditionMap->setJoin(array(StoreUserBean::$tableName,StoreUserBean::$alias,StoreRoleBean::$alias.POINST.StoreRoleBean::$id,StoreUserBean::$alias.POINST.StoreUserBean::$storeRoleId,'left'));
        $conditionMap->setGroup(StoreRoleBean::$alias.POINST.StoreRoleBean::$id);
        $conditionMap->setParameter(StoreRoleBean::$alias.POINST.StoreRoleBean::$storeId,$listMap->getParameter(GlobalBean::$storeId));
        $roleMap = StoreRoleModel::getInstance()->getJoinList($conditionMap);
        $roleMap ?: MyException::throwMyException("查询数据失败");
        return $roleMap;
    }

    /**
     * @desc 添加数据
     * @param StoreRoleBean $storeRoleBean
     * @return int int
     * @throws \Exception
     */
    public function add($storeRoleBean)
    {
        // TODO: Implement add() method.
        StoreRoleValidate::getInstance()->goCheck(StoreRoleValidate::$add,$storeRoleBean->getData());
        $storeRoleBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $result = StoreRoleModel::getInstance()->addDataGetInsertId($storeRoleBean->getData());
        $result ?: MyException::throwMyException("插入数据失败");
        return $result;
    }

    /**
     * @desc 编辑数据
     * @param StoreRoleBean $storeRoleBean
     * @return int int
     * @throws \Exception
     */
    public function edit($storeRoleBean)
    {
        // TODO: Implement edit() method.
        StoreRoleValidate::getInstance()->goCheck(StoreRoleValidate::$edit,$storeRoleBean->getData());
        $storeModel = StoreRoleModel::getInstance();
        $result = $storeModel->updatedData(array(StoreRoleBean::$id => $storeRoleBean->getId(),StoreRoleBean::$storeId => $storeRoleBean->getStoreId()),$storeRoleBean->getData());
        $result ?: MyException::throwMyException("更新失败");
        return $result;
    }

    /**
     * @desc 删除数据
     * @param StoreRoleBean $storeRoleBean
     * @return int int
     * @throws \Exception
     */
    public function delete($storeRoleBean)
    {
        // TODO: Implement delete() method.
        StoreRoleValidate::getInstance()->goCheck(StoreRoleValidate::$pkId,$storeRoleBean->getData());
        $storeModel = StoreRoleModel::getInstance();
        $userCount = StoreUserModel::getInstance()->getCount(array(StoreUserBean::$storeRoleId => $storeRoleBean->getId()));
        !$userCount ?: MyException::throwMyException("请先删除该角色用户");
        $result = StoreRoleModel::getInstance()->deleteData(array(StoreRoleBean::$id => $storeRoleBean->getId(),StoreRoleBean::$storeId => $storeRoleBean->getStoreId()));
        $result ?: MyException::throwMyException("删除角色失败");
        return true;
    }

    /**
     * @desc 根据主建获取数据
     * @param StoreRoleBean $storeRoleBean
     * @return StoreRoleBean
     * @throws \Exception
     */
    public function getInfo($storeRoleBean)
    {
        // TODO: Implement getInfo() method.
        StoreRoleValidate::getInstance()->goCheck(StoreRoleValidate::$pkId,$storeRoleBean->getData());
        $storeModel = StoreRoleModel::getInstance();
        $result = $storeModel->selectData(array(StoreRoleBean::$id => $storeRoleBean->getId(),StoreRoleBean::$storeId => $storeRoleBean->getStoreId()));
        $result ?:MyException::throwMyException("获取角色信息失败");
        $storeRoleBean->setData($result);
        return $storeRoleBean;
    }
}